package net.dotool.file;

import java.io.File;
import java.nio.file.Path;
import java.nio.file.WatchEvent;

/**
 * 监控文件的变化
 * @author 李岩飞
 * @email eliyanfei@126.com	
 * 2018年1月15日 下午8:51:40
 * 
 */
public class WatchTools {
	/**
	 * 监控文件目录变化
	 */
	public static WatchMonitor watchDir(String path, IWatch watch, WatchEvent.Kind<?>... events) {
		return watchPath(new File(path).toPath(), watch, null, events);
	}

	/**
	 * 监控文件变化
	 */
	public static WatchMonitor watchFile(String path, IWatch watch, WatchEvent.Kind<?>... events) {
		File file = new File(path);
		return watchPath(file.getParentFile().toPath(), watch, file.getName(), events);
	}

	private static WatchMonitor watchPath(Path path, IWatch watch, String fileName, WatchEvent.Kind<?>... events) {
		return new WatchMonitor(path, watch, fileName, events);
	}
	
	public static void main(String[] args) {
		WatchTools.watchFile("E:\\tmp\\aaa.txt", new SimpleWatch(){
			public void onModify(WatchEvent<?> event, Path currentPath) {
				System.out.println(currentPath.toString());
			}
		}).start();;
	}
}
